package com.amithkoujalgi.framework.rest.util;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsonschema2pojo.SchemaMapper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.codemodel.JCodeModel;

public class JSONUtil {
	private static Logger logger = Logger.getLogger(JSONUtil.class);

	private JSONUtil() {
	}

	public static String toJSON(Object o) {
		return new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
				.create().toJson(o);
	}

	public static <T> Object parse(String str, Class<T> clazz) {
		return new Gson().fromJson(str, clazz);
	}

	public static void jsonToPojo(String json, String className,
			String pkgName, String destDir) throws IOException {
		File dest = new File(destDir);
		if (!dest.exists()) {
			logger.info("Created dir: " + dest.getAbsolutePath());
		}
		JCodeModel codeModel = new JCodeModel();
		new SchemaMapper().generate(codeModel, className, pkgName, json);
		codeModel.build(dest);
	}
}
